package com.foreveross.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foreveross.model.ProxyIpModel;
import com.google.common.collect.Lists;

/**
 * Created by Bright on 16/3/1.
 */
public class ProxyIpUtil {


    public Logger logger = LoggerFactory.getLogger(ProxyIpUtil.class);
    private static final String IP_REGEX = "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{1,5}$";

    // IP下载重试次数。
    private final static int c = 10;

    private final static int timeout = 10000;

    /* 单例 */
    private static ProxyIpUtil _instance = null;

    public synchronized static ProxyIpUtil getInstance() {
        if (_instance == null) {
            _instance = new ProxyIpUtil();
        }
        return _instance;
    }

    /**
     * 随机从各IP提供商下载指定数量Ip 。
     *
     * @param num
     * @return
     * @throws Exception
     */
    public List<ProxyIpModel> downloadIps(int num) throws Exception {
        List<ProxyIpModel> proxyList = new ArrayList();
    	
    	List<String> proxyips = new ArrayList<String>();
        logger.info("开始下载代理IP");
        String url = "http://121.43.57.111:8015/tkip/tkip.html?size={num}";
        proxyips = downloadProxyIpsByUrl(url, num); // 下载国内IP。
      
        if(proxyips.size()>0){
        	for(String proxyip: proxyips){
        		String[] ip = proxyip.split(":");
        		String address = ip[0];
        		String port = ip[1];
        		ProxyIpModel proxy = new ProxyIpModel();
        		proxy.setIp(address);
        		proxy.setPort(Integer.valueOf(port));
        		proxyList.add(proxy);
        	}
        	
        	
        }else{
        	proxyList.addAll(downloadIps(100));
        	 logger.warn("IP提取失败！");
        }
        
        
        return proxyList;
    }


    /**
     * 下载代理IP。
     *
     * @param url
     * @return
     * @throws Exception
     */
    private List<String> downloadProxyIpsByUrl(String url, int num) throws Exception {
        List<String> proxyips = Lists.newArrayList();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
        httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
        String html;
        try {
            url = url.replace("{num}", String.valueOf(num));
            html = EntityUtils.toString(httpClient.execute(new HttpGet(url)).getEntity());
            if (org.apache.commons.lang3.StringUtils.isNotBlank(html)) {
                for (String s : html.split("\n")) {
                    if (org.apache.commons.lang3.StringUtils.isNotEmpty(s) && s.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{2,4}")) {
                        proxyips.add(s.replaceAll("\\s", ""));
                    }
                }
            }
            logger.info(String.format("本次提取数量:%s", proxyips.size()));
        } catch (Exception e) {
            //throw e;

        	Thread.sleep(2000);

        	proxyips.addAll(downloadProxyIpsByUrl(url,num));
        }
        return proxyips;
    }

    public static void main(String[] arg) throws Exception {

        List<ProxyIpModel> proxyIps = ProxyIpUtil.getInstance().downloadIps(10);
        System.out.println(String.format("提取数量:%s, 获得数量:%s", 1, proxyIps.size()));
//        for (String ip : proxyIps) {
//            System.out.print("[" + ip + "] ");
//            System.out.println();
//        }
    }

}
